// PartAttachView.cpp : implementation file
//

#include "stdafx.h"
#include "resourcedefine.h"
#include "PartAttachView.h"
#include "phsfactory.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPartAttachView

IMPLEMENT_DYNCREATE(CPartAttachView, CFormViewEx)

CPartAttachView::CPartAttachView()
	: CFormViewEx(CPartAttachView::IDD)
{
	INIT_RESOURCE
	//{{AFX_DATA_INIT(CPartAttachView)
	m_strCustomID = _T("");
	m_strDia = _T("");
	m_strMaterial = _T("");
	m_strName = _T("");
	m_strSpec = _T("");
	m_strTemperature = _T("");
	m_strWeight = _T("");
	m_strClgg = _T("");
	//}}AFX_DATA_INIT

	m_pObject = NULL;
	m_bIsFirstVisible = TRUE;
}

CPartAttachView::~CPartAttachView()
{
}

void CPartAttachView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPartAttachView)
	DDX_Text(pDX, IDC_CUSTOMID_EDIT, m_strCustomID);
	DDX_CBString(pDX, IDC_DIA_COMBO, m_strDia);
	DDX_CBString(pDX, IDC_MATERIAL_COMBO, m_strMaterial);
	DDX_Text(pDX, IDC_NAME_EDIT, m_strName);
	DDX_CBString(pDX, IDC_SPEC_COMBO, m_strSpec);
	DDX_Text(pDX, IDC_TEMPERATURE_EDIT, m_strTemperature);
	DDX_Text(pDX, IDC_WEIGHT_EDIT, m_strWeight);
	DDX_Text(pDX, IDC_CLGG_EDIT, m_strClgg);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPartAttachView, CFormViewEx)
	//{{AFX_MSG_MAP(CPartAttachView)
	ON_WM_CREATE()
	ON_WM_SHOWWINDOW()
	ON_CBN_SELCHANGE(IDC_DIA_COMBO, OnSelchangeDiaCombo)
	ON_EN_CHANGE(IDC_TEMPERATURE_EDIT, OnChangeTemperatureEdit)
	ON_CBN_SELCHANGE(IDC_SPEC_COMBO, OnSelchangeSpecCombo)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPartAttachView diagnostics

#ifdef _DEBUG
void CPartAttachView::AssertValid() const
{
	CFormView::AssertValid();
}

void CPartAttachView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CPartAttachView message handlers

int CPartAttachView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CFormView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	END_RESOURCE
	
	this->m_part.CloseGetPropertyTrigger();
	this->m_part.SetStandard(m_pObject->GetStandard());
	this->m_part.SetTemperature(m_pObject->GetTemperature());
	this->m_part.SetUnitWeight(m_pObject->GetUnitWeight());
	this->m_part.SetCustomID(m_pObject->GetCustomID());
	this->m_part.SetDiameter(m_pObject->GetDiameter());
	this->m_part.SetMaterial(m_pObject->GetMaterial());
	this->m_part.SetName(m_pObject->GetName());
	this->m_part.OpenGetPropertyTrigger();
	this->m_part.SetBH(m_pObject->GetBH());

	m_strTemperature.Format(_T("%g"), m_pObject->GetTemperature());
	m_strWeight.Format(_T("%g"), m_pObject->GetUnitWeight());
	m_strCustomID = m_pObject->GetCustomID();
	m_strDia.Format(_T("%g"),m_pObject->GetDiameter());
	m_strMaterial = m_pObject->GetMaterial();
	m_strName = m_pObject->GetName();
	m_strSpec = m_pObject->GetBH();
	m_strTableName = m_pObject->GetTableName();
	m_strObservation = m_pObject->GetObservation();
	m_strClgg = m_pObject->GetSpec();

	if(m_strObservation.IsEmpty())
	{
		m_strObservation = _T("0");
	}

	return 0;
}

void CPartAttachView::LoadDia()
{
	CStringArray* pArr = NULL;
	int nCount = 0;

	CComboBox* pDia = static_cast<CComboBox*>(GetDlgItem(IDC_DIA_COMBO));
	if(pDia == NULL)
	{
		return ;
	}
	pDia->ResetContent();
	
	pArr = GetCurDatabase().LoadDiameter(this->m_pObject->GetStandard(),
										 this->m_strTableName,
										 this->m_strCustomID,
										 this->m_strSpec);
	if(pArr == NULL)
	{
		return;
	}
	for(int i = 0; i < pArr->GetSize(); i++)
	{
		int j = pDia->AddString(pArr->GetAt(i));
		if(m_strDia == pArr->GetAt(i))
		{
			nCount = j;
		}
	}
	delete pArr;

	if(CB_ERR == pDia->SetCurSel(nCount))
		return;
	pDia->GetWindowText(m_strDia);

}

void CPartAttachView::LoadMaterial()
{
	CStringArray* pArr = NULL;
	int nCount = 0;
	
	CComboBox* pMaterialCombo = static_cast<CComboBox*>(GetDlgItem(IDC_MATERIAL_COMBO));
	if(pMaterialCombo == NULL)
	{
		return;
	}
	pMaterialCombo->ResetContent();
	
	pArr = GetCurDatabase().LoadMaterial(this->m_strCustomID,
										 this->m_strObservation,
										 atof(m_strTemperature));
	if(pArr == NULL)
	{
		return;
	}
	for(int i = 0; i < pArr->GetSize(); i++)
	{
		int j = pMaterialCombo->AddString(pArr->GetAt(i));
		if(m_strMaterial == pArr->GetAt(i))
		{
			nCount = j;
		}
	}
	delete pArr;
	
	if(CB_ERR == pMaterialCombo->SetCurSel(nCount))
		return;
	pMaterialCombo->GetWindowText(m_strMaterial);

}

void CPartAttachView::LoadBH()
{
	CStringArray* pArr = NULL;
	INT nCount = 0;

	CComboBox* pSpecCombo = static_cast<CComboBox*>(GetDlgItem(IDC_SPEC_COMBO));
	if(pSpecCombo == NULL)
	{
		return;
	}
	pSpecCombo->ResetContent();
	
	pArr = GetCurDatabase().LoadBH(this->m_pObject->GetStandard(),this->m_strTableName, this->m_strCustomID);
	if(pArr == NULL)
	{
		return;
	}
	for(int i = 0; i < pArr->GetSize(); i++)
	{
		int j = pSpecCombo->AddString(pArr->GetAt(i));
		if(m_strSpec == pArr->GetAt(i))
			nCount = j;
	}
	delete pArr;
	
	if(CB_ERR == pSpecCombo->SetCurSel(nCount))
		return;
	pSpecCombo->GetWindowText(m_strSpec);

	LoadDia();
}

void CPartAttachView::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CFormView::OnShowWindow(bShow, nStatus);
	
	if(m_bIsFirstVisible)
	{
		UpdateData(FALSE);
		LoadBH();
		LoadMaterial();
		LoadSpec();
		m_bIsFirstVisible = FALSE;
	}
	
/*	if(!bShow)
	{
		UpdateObjectData();
	}*/
}

void CPartAttachView::OnSelchangeDiaCombo() 
{
	LoadSpec();
}

void CPartAttachView::OnChangeTemperatureEdit() 
{
	UpdateData();
	LoadMaterial();
	LoadSpec();
}

void CPartAttachView::LoadOthers()
{
	m_part.CloseGetPropertyTrigger();
	m_part.SetBH(this->m_strSpec);
	m_part.SetCustomID(this->m_strCustomID);
	m_part.OpenGetPropertyTrigger();
	m_part.SetMaterial(this->m_strMaterial);

	this->m_strWeight.Format(_T("%g"), m_part.GetUnitWeight());

	UpdateData(FALSE);
}

void CPartAttachView::OnSelchangeSpecCombo() 
{
	UpdateData();
	LoadDia();
	LoadOthers();
	LoadSpec();
}


void CPartAttachView::UpdateObjectData()
{
	UpdateData();
	
	m_pObject->CloseGetPropertyTrigger();
	m_pObject->SetDiameter(atof(m_strDia));
	m_pObject->SetTemperature(atof(m_strTemperature));
	m_pObject->OpenGetPropertyTrigger();
	m_pObject->SetUnitWeight(atof(m_strWeight));
	m_pObject->SetMaterial(m_strMaterial);
}

void CPartAttachView::OnDestroy() 
{
	CFormView::OnDestroy();
	
	UpdateObjectData();
	
}

void CPartAttachView::LoadSpec()
{
	this->LoadOthers();
	this->m_strClgg = m_part.GetSpec();
	this->UpdateData(FALSE);
}